package com.linxz.framework.ui.loader;
import android.app.Dialog;
import android.content.Context;
/**
 * @author Linxz
 * 创建日期：2020年12月14日 10:37
 * version：
 * 描述：
 */
public class LoadingView {

    public enum LOADING_TYPE {
        /**
         * 自定义，需要传入Dialog
         */
        CUSTOMER,
        /**
         * framework自定义Dialog
         */
        DEFAULT,
        /**
         * AVLoad第三方库
         */
        AV_LOADER
    }

    private LOADING_TYPE loadingType = LOADING_TYPE.DEFAULT;
    private static Dialog customerLoader;
    private DefaultLoadingProgress defaultDialog;
    private String defaultIndicator = LoaderStyle.BallClipRotatePulseIndicator.name();
    private int indicatorColor = 0;

    private static class Holder {
        private static final LoadingView INSTANCE = new LoadingView();
    }

    /**
     * 可以在application初始化
     */
    public static LoadingView init() {
        return Holder.INSTANCE;
    }

    public static LoadingView getInstance(){
        return Holder.INSTANCE;
    }

    public void setLoadingType(LOADING_TYPE type) {
        this.loadingType = type;
    }

    public void withCustomerLoader(Dialog dialog) {
        customerLoader = dialog;
    }

    public void withAVIndicator(String indicatorName) {
        defaultIndicator = indicatorName;
    }

    public void withAVIndicatorColor(int color){
        this.indicatorColor=color;
    }

    public void showLoading(Context context, String msg) {
        switch (loadingType) {
            case CUSTOMER:
                if (customerLoader != null) {
                    customerLoader.show();
                } else {
                    throw new NullPointerException("LOADING_TYPE is CUSTOMER,but customerLoader is null.");
                }
                break;
            case DEFAULT:
                if (defaultDialog == null) {
                    defaultDialog = DefaultLoadingProgress.newInstance(context, msg, true, false, null);
                }
                defaultDialog.show();
                break;
            case AV_LOADER:
                AVLoader.showLoading(context, defaultIndicator,indicatorColor);
                break;
            default:
                break;
        }
    }

    public void showLoading(Context context) {
        switch (loadingType) {
            case CUSTOMER:
                if (customerLoader != null) {
                    customerLoader.show();
                } else {
                    throw new NullPointerException("LOADING_TYPE is CUSTOMER,but customerLoader is null.");
                }
                break;
            case DEFAULT:
                if (defaultDialog == null) {
                    defaultDialog = DefaultLoadingProgress.newInstance(context, "", true, false, null);
                }
                defaultDialog.show();
                break;
            case AV_LOADER:
                AVLoader.showLoading(context, defaultIndicator,indicatorColor);
                break;
            default:
                break;
        }
    }

    public void hideLoading(){
       if (customerLoader!=null){
           customerLoader.dismiss();
       }
       if (defaultDialog!=null){
           defaultDialog.dismiss();
           defaultDialog=null;
       }
        AVLoader.stopLoading();
    }
}
